<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>
<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import { getDateStr, getDaysPriorDateStr } from '@/utils/commonUtils';
import { listOrderTjPm } from "@/api/business/order";
const animationDuration = 3000
export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null,
      showList: [],
      payList: [],
      marketList: []
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initValue()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initValue() {
      let date1 = getDaysPriorDateStr(new Date(),6);
      let date7 = getDateStr(new Date());      
      let poolParam = {createAtstart: date1, createAtend: date7};
      this.showList = [];
      this.payList = [];
      this.marketList = [];
      listOrderTjPm(poolParam).then(response => {
        response.data.forEach(item => {
          this.showList.push({name: item.shopName,  max: item.amount});
          this.payList.push(item.payAmount);
          this.marketList.push(item.marketAmount);
        })
        if (this.showList.length<6) {
          let num = 6-this.showList.length;
          for (let i=0; i<num; i++) {
            this.showList.push({name: '',  max: 0});
            this.payList.push(0);
            this.marketList.push(0);
          }
        }
      });      
      setTimeout(this.initChart, 4000);
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        radar: {
          radius: '66%',
          center: ['50%', '50%'],
          splitNumber: 8,
          splitArea: {
            areaStyle: {
              color: 'rgba(127,95,132,.3)',
              opacity: 1,
              shadowBlur: 45,
              shadowColor: 'rgba(0,0,0,.5)',
              shadowOffsetX: 0,
              shadowOffsetY: 15
            }
          },
          indicator: this.showList
        },
        legend: {
          left: 'center',
          bottom: '10',
          data: ['销售', '分销']
        },
        series: [{
          type: 'radar',
          symbolSize: 0,
          areaStyle: {
            normal: {
              shadowBlur: 13,
              shadowColor: 'rgba(0,0,0,.2)',
              shadowOffsetX: 0,
              shadowOffsetY: 10,
              opacity: 1
            }
          },
          data: [
            {
              value: this.payList,
              name: '销售'
            },
            {
              value: this.marketList,
              name: '分销'
            }
          ],
          animationDuration: animationDuration
        }]
      })
    }
  }
}
</script>
